<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=8">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta content="B2B电商平台" name="description"/>
<meta content="B2B电商平台" name="keyword"/>

<title>B2B电商平台.收/发货地址维护</title>

<link rel="stylesheet" type="text/css" href="<%=path %>/include/css/global.css" />
<link rel="stylesheet" type="text/css" href="<%=path %>/include/css/themes.css" />
<link rel="stylesheet" type="text/css" href="<%=path %>/include/css/proManage.css" />
<link rel="stylesheet" type="text/css" href="<%=path %>/include/css/style.css" />
<link rel="stylesheet" type="text/css" href="<%=path %>/include/css/btn.css" />
<link rel="stylesheet" type="text/css" href="<%=path %>/include/css/mmt_trading.css" />
<link rel="stylesheet" type="text/css" href="<%=path%>/include/pagination/page.css" media="all">
<link rel="stylesheet" type="text/css" href="<%=path %>/include/sweetalert/sweetalert.css" />
<script type="text/javascript" src="<%=path %>/include/js/jquery-1.8.0.min.js"></script>
<script type="text/javascript" src="<%=path %>/include/js/cookies.js"></script>
<script type="text/javascript" src="<%=path%>/include/pagination/jquery.pagination.js"></script>
<script type="text/javascript" src="<%=path%>/include/pagination/init.js"></script>
<script type="text/javascript" src="<%=path %>/include/js/icss.common.js"></script>
<script type="text/javascript" src="<%=path %>/include/sweetalert/sweetalert.min.js"></script>
<script type="text/javascript" src="<%=path %>/include/sweetalert/sweetalert-dev.js"></script>
<style type="text/css">
.save{
    width:44px;
    height:24px;
    line-height:24px;   
    background-color: transparent;
    background-repeat: no-repeat;
    background-position: 0 -203px;
    border:0;
    cursor:pointer;
    font: 12px/1.5 arial,tahoma,\5b8b\4f53;
    margin: 0 0 0 5px;
    outline:none;
    background-image: url('<%=path%>/include/css/img/xs_bgx.png');   
}
.left{
	width:16%;
	font-size:14px;
	padding:6px 16px 6px 0px;
	vertical-align: middle;
	text-align:right;
}
.left_textarea{
	width:16%;
	font-size:14px;
	padding:6px 16px 6px 0px;
	vertical-align: top;
	text-align:right;
}
.right{
	width:84%;
	font-size:14px;
	padding:6px 0px 6px 16px;
	vertical-align: middle;
	text-align:left;
}
.titleName{
	width:10%;
	color:#FC6210;
	vertical-align: middle;
	text-align:right;
	line-height:26px;
	height:26px;
	font-weight:bold;
	font-size:15px;
	padding-right:16px;
}
.tips-errow,.tips-ok,.tipsCur{padding-left:10px;font-size:12px;height:20px;line-height:20px;}
.promptBox{display:inline-block;width:200px;white-space:nowrap; position:relative;z-index:5;}
.tips-errow{
	color:#FF3030;
}
.tipsCur{
	color:#B9D3EE;
}
</style>
<script type="text/javascript">
	$(function(){
		$.ajaxSetup({cache:false});
		/*输入框内容改变事件，内容改变，隐藏提示*/
		$(".right input").live("change", function() {
			var thisObj = jQuery(this);
			thisObj.next("div").find(".tips-errow").hide();
			thisObj.next("div").find(".tipsCur").hide();
			thisObj.next("div").find(".tips-ok").hide();
		});
		$(".right textarea").live("change", function() {
			var thisObj = jQuery(this);
			thisObj.next("div").find(".tips-errow").hide();
			thisObj.next("div").find(".tipsCur").hide();
			thisObj.next("div").find(".tips-ok").hide();
		});
		$("select").live("change", function() {
			var locationCountry = $("#locationCountry").val();	//国家
			var locationProvince = $("#locationProvince").val();//省
			var locationCity = $("#locationCity").val();		//市/县
			var locationArea = $("#locationArea").val();		//区
			if(locationCountry != ''&& locationProvince != ''&& locationCity !=''&& locationArea !=''){ 	
				$("#areaTip").find(".tips-ok").hide();
		 	  	$("#areaTip").find(".tips-errow").hide();
				$("#areaTip").find(".tipsCur").hide();
			}
		});
		initOtherArea('','');
		_queryList();
	})
	//查询广告信息
	var pagination; //分页
	var addEditFlag; //添加或修改标识
	function _queryList(){
		var url = "<%=path %>/delivery/queryDeliveryList.do";
  		pagination = _initPagination({'pageId':'pagination', 'cKey':'toAdInfo','url': url,'rows': 8, 'callback':_initDataList});
  		
	}
	function _initDataList(data) {
	 		var d = data.rows;
	 		var total = data.total;
	 		$("#saveNum").html(total);
	 		var leftNum = 20-parseInt(total);
	 		$("#leftCnt").html(leftNum);
			var str = "";
			$("#deliveryList").empty();
			for(var i=0;i<d.length;i++){
				str += "<tr>";
				str += "    <td  style='width:11%;text-align:center;vertical-align: middle;padding:1px;'>"+d[i].user_name+"</td>";
				str += "    <td  style='width:15%;text-align:center;vertical-align: middle;padding:1px;'>"+d[i].countryName+"&nbsp;~&nbsp;"+d[i].provinceName+"&nbsp;~&nbsp;"+d[i].cityName+"&nbsp;~&nbsp;"+d[i].areaName+"</td>";
				str += "    <td  style='width:15%;text-align:center;vertical-align: middle;padding:1px;'>"+d[i].streetaddress+"</td>";
				str += "    <td  style='width:11%;text-align:center;vertical-align: middle;padding:1px;'>"+d[i].postcode+"</td>";
				str += "    <td  style='width:11%;text-align:center;vertical-align: middle;padding:1px;'>"+d[i].phone_number+"</td>";
				str += "    <td  style='width:11%;text-align:center;vertical-align: middle;padding:1px;'>"+d[i].corporate_name+"</td>";
				str +=" 	<td  style='width:20%;text-align:center;vertical-align: middle;padding:1px;'>";
				str +="    		<a href='javaScript:void(0);' onclick=\"editAddress('" + d[i].deliveryid +"')\" >修改</a>";
				str +="    		/&nbsp;<a href='javaScript:void(0);' onclick=\"delAddress('" + d[i].deliveryid +"')\" >删除</a>";
				str +=" 	</td>";
				str +=" </tr>";
			}
			$("#deliveryList").append(str);
			
	 }
	 
	function initOtherArea(areaCode,level){
		$.ajax({
              cache: true,
              type: "POST",
              url:'<%=path%>/delivery/initOtherArea.do',
              data:{code:areaCode,level:level},
              async: false,
              error: function(request) {
                 swal("系统繁忙，请稍候重试!","", "error");	
              },
              success: function(data) {
				data = eval('(' + data + ')');
				var str='';
				if(data.length>0){
					level=data[0].LEVEL;
					if(level=='1'){
						str +='<option  value="">请选择国家</option>';
					}else if(level=='2'){
						str +='<option  value="">请选择省/直辖市</option>';
					}else if(level=='3'){
						str +='<option  value="">请选择市/县</option>';
					}else if(level=='4'){
						str +='<option  value="">请选择区</option>';
					}
					for(var i=0; i<data.length; i++){
						str +='<option  value="'+data[i].AREA_CODE+'">'+data[i].AREA_NAME+'</option>';
					}
					
					if(level=='1'){
						$("#locationCountry").empty();
						$("#locationProvince").empty();
						$("#locationCity").empty();
						$("#locationArea").empty();
						$("#locationCountry").append(str);
						$('#locationCountry').change(function(){   
							var areaCode=$(this).find("option:selected").val();
							initOtherArea(areaCode,level);   
				  		})
					}else if(level=='2'){
						$("#locationProvince").empty();
						$("#locationCity").empty();
						$("#locationArea").empty();
						$("#locationProvince").append(str);
						$('#locationProvince').change(function(){   
							var areaCode=$(this).find("option:selected").val();
							initOtherArea(areaCode,level);  
				  		})
					}else if(level=='3'){
						$("#locationCity").empty();
						$("#locationArea").empty();
						$("#locationCity").append(str);
						$('#locationCity').change(function(){   
							var areaCode=$(this).find("option:selected").val();
							initOtherArea(areaCode,level); 
				  		})
					}else{
						$("#locationArea").empty();
						$("#locationArea").append(str);
						$('#locationArea').change(function(){   
							var areaCode=$(this).find("option:selected").val();
							initOtherArea(areaCode,level);  
				  		})
					}
				}
              }
         });
	}
	 
	//修改地址前，查询要修改的地址信息
	function editAddress(deliveryid){
		$.ajax({
              cache: true,
              type: "POST",
              url:'<%=path%>/delivery/queryTheAddress.do?deliveryid='+deliveryid,
              async: false,
              error: function(request) {
                 swal("系统繁忙，请稍候重试!","", "error");	
              },
              success: function(data) {
				data = eval('(' + data + ')');
				$("#deliveryid").val(data.deliveryid);
				$("#userName").val(data.user_name);	
				$("#locationCountry").val(data.location_country);
				$("#locationProvince").val(data.location_province);
				$("#locationCity").val(data.location_city);
				$("#locationArea").val(data.location_area);
				$("#streetAddress").val(data.streetaddress);
				$("#postcode").val(data.postcode);
				$("#phoneNumber").val(data.phone_number);
				$("#corporateName").val(data.corporate_name);
				$("#remark").val(data.remark);
				if(data.ifdefault=='1'){
					$("#ifCheck").attr("checked",true);
				}else{
					$("#ifCheck").attr("checked",false);
				}
				  
              }
         });
	}
	//删除地址
	function delAddress(deliveryid){
		 parent.swal({
			title: "您确定要删除吗？", 
			type: "warning",
			showCancelButton: true,
			closeOnConfirm: false,
			cancelButtonText: "取消",
			cancelButtonColor: "#ec6c62",
			confirmButtonText: "确定",
			confirmButtonColor: "#ec6c62"
			}, function() {
				$.ajax({
	                cache: true,
	                type: "POST",
	                url: "<%=path%>/delivery/delAddress.do?deliveryid="+deliveryid,
	                async: false,
	                error: function(request) {
	                  alert('服务器拒绝访问!');  
	                },
	          		success: function(data) {
						data = eval('(' + data + ')');
						var msg = data.msg;
		                if(data.success){
							swal(msg,"", "success");
		                  	_queryList();
						}else {
							swal(msg,"", "error");
						}
		             }
	            });
			});
	}
	
	//保存地址信息(修改或新增)
	function saveAddress(){
		var deliveryid=$("#deliveryid").val();
		if(deliveryid==''){
			addEditFlag = 'add';
		}else{
			addEditFlag = 'edit';
		}
		if($("#ifCheck").is(':checked')){
			$("#ifDefault").val('1');
		}else{
			$("#ifDefault").val('0');
		}
		if(_checkIfTrue()){
				swal({
					title: "确定保存吗？", 
					type: "warning",
					showCancelButton: true,
					closeOnConfirm: false,
					cancelButtonText: "取消",
					cancelButtonColor: "#ec6c62",
					confirmButtonText: "确定",
					confirmButtonColor: "#ec6c62"
					}, function() {
						$(".tt").each(function() {
								var rule = $(this).attr("rule");
								var array = rule.split("_");
								var obj = array[0];
								pro = array[1];
								$(this).attr("name", obj + "." + pro);
						});
						$.ajax({
				              cache: true,
				              type: "POST",
				              url:'<%=path%>/delivery/saveAddress.do?addEditFlag='+addEditFlag,
				              data:$('#addressfm').serialize(),
				              async: false,
				              error: function(request) {
				                 swal("系统繁忙，请稍候重试!","", "error");	
				              },
				              success: function(data) {
								data = eval('(' + data + ')');
								var msg = data.msg;
				                if(data.success){
									swal(msg,"", "success");
				                    location.reload();
								}else {
									swal(msg,"", "error");
								}
				              }
				         });
					});
			}
	}
	
	//验证输入是否正确
	function _checkIfTrue(){
	 	var userName = $("#userName").val();			   //用户名
		var postcode  = $("#postcode ").val();			   //邮政编码
		var phoneNumber = $("#phoneNumber").val();		   //电话号码
		var locationCountry = $("#locationCountry").val();	//国家
		var locationProvince = $("#locationProvince").val();//省
		var locationCity = $("#locationCity").val();		//市/县
		var locationArea = $("#locationArea").val();		//区
		var streetAddress = $("#streetAddress").val();		//街道
		if(userName==''&&postcode ==''&&phoneNumber==''&&locationCountry == ''&& locationProvince == ''&& locationCity ==''&& locationArea=='' &&streetAddress ==''){ //验证
			$("#userName").next("div").find(".tips-ok").hide();
	 	  	$("#userName").next("div").find(".tips-errow").hide();
			$("#userName").next("div").find(".tipsCur").show();
			$("#postcode").next("div").find(".tips-ok").hide();
	 	  	$("#postcode").next("div").find(".tips-errow").hide();
			$("#postcode").next("div").find(".tipsCur").show();
			$("#phoneNumber").next("div").find(".tips-ok").hide();
	 	  	$("#phoneNumber").next("div").find(".tips-errow").hide();
			$("#phoneNumber").next("div").find(".tipsCur").show();
			$("#areaTip").find(".tips-ok").hide();
	 	  	$("#areaTip").find(".tips-errow").hide();
			$("#areaTip").find(".tipsCur").show();
			$("#addressTip").find(".tips-ok").hide();
	 	  	$("#addressTip").find(".tips-errow").hide();
			$("#addressTip").find(".tipsCur").show();
			return false;
		}else if(userName==''){	 //验证是否填写用户帐号名
			$("#userName").next("div").find(".tips-ok").hide();
	 	  	$("#userName").next("div").find(".tips-errow").hide();
			$("#userName").next("div").find(".tipsCur").show();
			return false;
		}else if(locationCountry == ''|| locationProvince == ''|| locationCity ==''|| locationArea==''){ 	//验证所在地区
			$("#areaTip").find(".tips-ok").hide();
	 	  	$("#areaTip").find(".tips-errow").hide();
			$("#areaTip").find(".tipsCur").show();
			 return false;
		}else if(streetAddress == ''){
			$("#addressTip").find(".tips-ok").hide();
	 	  	$("#addressTip").find(".tips-errow").hide();
			$("#addressTip").find(".tipsCur").show();
			return false;
		}else if(postcode==''){ //验证是否填写邮政编码
			$("#postcode").next("div").find(".tips-ok").hide();
	 	  	$("#postcode").next("div").find(".tips-errow").hide();
			$("#postcode").next("div").find(".tipsCur").show();
			return false;
		}else if(phoneNumber==''){	//验证是否填写电话号码
			$("#phoneNumber").next("div").find(".tips-ok").hide();
	 	  	$("#phoneNumber").next("div").find(".tips-errow").hide();
			$("#phoneNumber").next("div").find(".tipsCur").show();
			return false;
		}else if(phoneNumber!=''){
			var reg = /^1[3|4|5|7|8][0-9]{9}$/; //验证手机号码规则
			var flag = reg.test(phoneNumber);
			if(flag){
				$("#phoneNumber").next("div").find(".tips-ok").hide();
		 	  	$("#phoneNumber").next("div").find(".tips-errow").hide();
				$("#phoneNumber").next("div").find(".tipsCur").hide();
			}else{
				$("#phoneNumber").next("div").find(".tips-ok").hide();
		 	  	$("#phoneNumber").next("div").find(".tips-errow").show();
				$("#phoneNumber").next("div").find(".tipsCur").hide();
			}
			return flag; 
		}
		return true;
	}
	
	function _reHeight() {
		var main = $(window.parent.document).find("#rightFrame");
		var mainheight = $("#contentDiv").height() + 40;
		main.height(mainheight);
	}
</script>
</head>
<body style="font-family: '微软雅黑';padding: 0px;">
	<div id="contentDiv" style="padding:5px; width:99%;">
	 	<div style="width:100%;height:30px;border-bottom:1px solid #ADADAD;">
	      	<div class="toptil">收/发货地址维护</div>
	    </div>
	    <div class="colnums-2-ab-b">
	    	<form id="addressfm" name="addressfm" method="post" action="" novalidate="novalidate">
	    		<input type="hidden" name="deliveryid" id="deliveryid"  class="tt"  rule="deliveryInfo_deliveryid"/>
	    		<input type='hidden' class="tt" name='ifDefault' value='1' id='ifDefault'  rule="deliveryInfo_ifDefault" >
	    		<div  style="width:100%;margin-top:16px;margin-left:10px;">
		    		<table style="width:100%;">
		    			<tr>
		    				<td class="titleName">添加新地址:</td>
		    				<td class="right">备注和公司名称为可选项，其余均为必填项</td>
		    			</tr>
		    			<tr>
		    				<td class="left">联系人:</td>
		    				<td class="right">
		    					<input class="tt" style="width:30%;height:24px;" id="userName" name="userName"   rule="deliveryInfo_userName"  value="">
		    					<div class="promptBox">
                        			<div class='tips-errow' style="display:none;">请输入联系人</div>
                        			<div class='tips-ok' style="display:none;"></div>
                        			<div class='tipsCur' style="display:none;">请输入联系人</div>
                        		</div>
		    				</td>
		    			</tr>
		    			<tr>
		    				<td class="left">所在地区:</td>
		    				<td class="right">
	                            <select name="locationCountry" placeholder="国家" class="tt" style="height: 24px;width: 130px;"  rule="deliveryInfo_locationCountry" id="locationCountry" >
	                          		 <option value=""></option>
	                            </select>&nbsp;
	                            <select name="locationProvince" placeholder="省/直辖市"  class="tt" style="height: 24px;width: 130px;"  rule="deliveryInfo_locationProvince" id="locationProvince" >
									<option value=""></option>
	                            </select>&nbsp;
	                            <select name="locationCity"  placeholder="市/县/辖区" class="tt" style="height: 24px;width: 130px;"  rule="deliveryInfo_locationCity" id="locationCity" >
									<option value=""></option>
	                            </select>&nbsp;
	                            <select name="locationArea"  placeholder="区" class="tt" style="height: 24px;width: 130px;"  rule="deliveryInfo_locationArea" id="locationArea" >
									<option value=""></option>
	                            </select>
	                            <div class="promptBox" id="areaTip">
                        			<div class='tips-errow' style="display:none;">请完整选择所在地区</div>
                        			<div class='tips-ok' style="display:none;"></div>
                        			<div class='tipsCur' style="display:none;">请完整选择所在地区</div>
                        		</div>
		    				</td>
		    			</tr>
		    			<tr>
		    				<td class="left_textarea">街道地址:</td>
		    				<td class="right">
		    					<textarea id="streetAddress" class="tt" rule="deliveryInfo_streetAddress" placeholder="建议您如实填写详细收获地址,例如街道名称,门牌号码,楼层和房间号等信息" name="streetAddress" style="width: 484px; height: 64px; resize: none;"></textarea>
		    					<div class="promptBox" id="addressTip">
                        			<div class='tips-errow' style="display:none;">请输入街道地址</div>
                        			<div class='tips-ok' style="display:none;"></div>
                        			<div class='tipsCur' style="display:none;">请输入街道地址</div>
                        		</div>
		    				</td>
		    			</tr>
		    			<tr>
		    				<td class="left">邮政编码:</td>
		    				<td class="right">
		    					<input class="tt"  rule="deliveryInfo_postcode" style="width:30%;height:24px;" id="postcode" name="postcode"  value="">
		    					<div class="promptBox">
                        			<div class='tips-errow' style="display:none;">邮政编码输入不符合规范</div>
                        			<div class='tips-ok' style="display:none;"></div>
                        			<div class='tipsCur' style="display:none;">请输入邮政编码</div>
                        		</div>
		    				</td>
		    			</tr>
		    			<tr>
		    				<td class="left">手机号码:</td>
		    				<td class="right">
		    					<input class="tt"  rule="deliveryInfo_phoneNumber" style="width:30%;height:24px;" id="phoneNumber" name="phoneNumber"  value="">
		    					<div class="promptBox">
                        			<div class='tips-errow' style="display:none;">手机号输入不符合规范</div>
                        			<div class='tips-ok' style="display:none;"></div>
                        			<div class='tipsCur' style="display:none;">请输入手机号码</div>
                        		</div>
		    				</td>
		    			</tr>
		    			<tr>
		    				<td class="left">公司名称:</td>
		    				<td class="right">
		    					<input class="tt"  rule="deliveryInfo_corporateName" style="width:30%;height:24px;" id="corporateName" name="corporateName" value="">
		    				</td>
		    			</tr>
		    			<tr>
		    				<td class="left_textarea">备注:</td>
		    				<td class="right">
		    					<textarea id="remark" class="tt" rule="deliveryInfo_remark" placeholder="请输入您需要备注的信息" name="remark" style="width: 484px; height: 64px; resize: none;"></textarea>
		    				</td>
		    			</tr>
		    			<tr>
		    				<td class="left"></td>
		    				<td class="right" style="font-size:13px;">
		    					<input type='checkbox' name='ifCheck' value='1' id='ifCheck' >设为默认收获地址
							</td>
		    			</tr>
		    			<tr>
		    				<td class="left"></td>
		    				<td class="right">
		    					<div id="btnDiv"> 
						   	 		<button class="save" type="button" onclick="saveAddress()">保存</button>
								</div>
							</td>
		    			</tr>
		    		</table>
	    		</div>
	    	</form>
	    	<div style="width:96%;padding:10px 0px 10px 42px;color:#FC6210;">
	    	已保存了<span id="saveNum" style="padding:4px;"></span>条地址，还能保存<span id="leftCnt" style="padding:4px;"></span>条地址
	    	</div>
	    	<!-- 表格部分开始 -->
			<div class="listed_tb_k" style="border:none;width: 90%;padding-left:42px;">
		        <table cellspacing="0" cellpadding="0">
		            <thead>
			            <tr>
			                <th style="text-align:center;vertical-align: middle" >联系人</th>
			                <th style="text-align:center;vertical-align: middle" >所在地区</th>
			                <th style="text-align:center;vertical-align: middle" >街道地址</th>
			                <th style="text-align:center;vertical-align: middle" >邮政编码</th>
			                <th style="text-align:center;vertical-align: middle" >手机号码</th>
			                <th style="text-align:center;vertical-align: middle" >公司名称</th>
			                <th style="text-align:center;vertical-align: middle" >操作</th>
			            </tr>
		            </thead>
		            <tbody id="deliveryList">
		            
					</tbody>
			    </table>
			    <br/>
				<div class="dataTables_paginate paging_full_numbers">
				</div>
				<div id="pagination" ></div>
		 	</div>
		 	<!-- 表格部分结束 -->
		 	<div class="clear"></div>
	   </div>
	   <div class="clear"></div>
	</div>
</body>
<script type="text/javascript">
	$(document).ready(function(){
		_reHeight();
	});
</script>
</html>
